package com.ktjiaoyu.controller;


import com.ktjiaoyu.entity.*;
import com.ktjiaoyu.service.AttrService;
import com.ktjiaoyu.service.AttrValueService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/main/attrValue")
public class AttrValueController extends BaseController{
    private static final Logger LOGGER= LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    private AttrValueService attrValueService;
    @Autowired
    private AttrService attrService;


    @RequestMapping("/page")
    public String page(Model model,
                       AttrValues condition,
                       @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        Page<AttrValues> page=null;
        try{
            page=attrValueService.getPage(condition,pageNo,pageSize);
            List<Attr> attrList=attrService.findAll();

            model.addAttribute("pageInfo",page);
            model.addAttribute("attrList",attrList);
            model.addAttribute("condition",condition);
        }catch (Exception e){
            LOGGER.error("SysUserController中的page方法执行错误.",e);
            throw new RuntimeException(e);
        }
        return "attrValue/page";
    }

    @GetMapping("/save")
    public String toSave(Model model){
        List<Attr>attrList=null;
        try{
            attrList=attrService.findAll();
            model.addAttribute("attrList",attrList);
            model.addAttribute("attrValue",new AttrValues());
        }catch (Exception e){
            LOGGER.error("SysUserController中的toSave方法执行错误",e);
            throw new RuntimeException(e);
        }
        return "attrValue/save";
    }


    @PostMapping("/save")
    public String doSave(AttrValues attrValues){
        try{
           Long orderNum=attrValueService.count()+1;
           attrValues.setOrderNum(orderNum);
            boolean ok=attrValueService.saveOrUpdate(attrValues);
            if(ok){
                return redirect("/main/attrValue/page");
            }
        }catch (Exception e){
            LOGGER.error("SysUserController中的doSave方法执行错误",e);
        }
        return "attrValue/save";
    }


    @GetMapping("/edit")
    public String toEdit(@RequestParam("id") Integer id,Model model){
        AttrValues attrValue=null;
        List<Attr> attrList=null;
        try{
            attrValue=attrValueService.getById(id);
            attrList=attrService.findAll();
            model.addAttribute("attrList",attrList);
            model.addAttribute("attrValue",attrValue);
        }catch (Exception e){
            LOGGER.error("SysUserController中的toEdit方法发生错误.",e);
            throw new RuntimeException(e);
        }
        return "attrValue/save";
    }


    @DeleteMapping("/del/{id}")
    @ResponseBody
    public String del(@PathVariable("id") Long id){
        try{
            int result=attrValueService.delete(id);
            return  ajaxResult();
        }catch (Exception e){
            LOGGER.error("del方法执行错误");
            throw new RuntimeException(e);
        }
    }
}
